# -*- coding: cp936 -*- 
import urllib2
import sys
import re
import base64
import json
def get(url,username,password,getsth):       #调用API获取粉丝或关注者的某些属性
    next_cursor='-1' #分页标志
    sthlist=[]
    while int(next_cursor):        
        theurl = url+'?source=227362026&cursor='+next_cursor+'&count=100' #调用微博api，227362026可改为自己申请的key
        base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
        authheader =  "Basic %s" % base64string
        req=urllib2.Request(theurl)
        req.add_header("Authorization", authheader)
        try:
            handle = urllib2.urlopen(req)
        except IOError, e:
            print "It looks like the username or password is wrong."
            sys.exit(1)
        a=json.loads(handle.read())  
        for user in a['users']:
            sthlist.append(user[getsth])
        next_cursor=str(a['next_cursor'])
    return sthlist

def followersname(): #得到粉丝列表
    return get('http://api.t.sina.com.cn/statuses/followers.json',name,password,'name')
def friendsname(): #得到用户关注列表
    return get('http://api.t.sina.com.cn/statuses/friends.json',name,password,'name')
#for follower in followers:
#    print follower
def huxiangguanzhuname(followers,friends):   
    huxiangguanzhu=[]
    for follower in followers:
        if follower in friends:
            huxiangguanzhu.append(follower)
    return huxiangguanzhu    #得到互相关注的名单
                 


if __name__ == '__main__':
    name=raw_input('输入账号：')
    password=raw_input('输入密码：')
    while True:
        print '*------------------*'   #太懒了，暂时就写这4个功能
        print '按数字键选择要查询的项目'
        print '1.互相关注'
        print '2.你关注他而他没关注你'
        print '3.他关注你而你没关注他'
        print '4.关注人和粉丝的男女比例'
        print '*------------------*'
        number=raw_input('输入数字选择项目：')
        if number=='1':
            print '正在分析，请稍后……'
            friends=friendsname()
            followers=followersname()
            huxiangguanzhu=huxiangguanzhuname(followers,friends)
            for a in huxiangguanzhu:
                print a
            print '共有'+str(len(huxiangguanzhu))+'人和你互相关注'
            q=raw_input('按"q"键返回上级菜单:')
            if q=='q':
                continue
        elif number=='2':
            print '正在分析，请稍后……'
            friends=friendsname()
            followers=followersname()
            huxiangguanzhu=huxiangguanzhuname(followers,friends)
            n=0
            for friend in friends:
                if friend not in huxiangguanzhu:
                    n+=1
                    print friend
            print '共有'+str(n)+'人你关注他而他没关注你'
            q=raw_input('按"q"键返回上级菜单:')
            if q=='q':
                continue
        elif number=='3':
            print '正在分析，请稍后……'
            friends=friendsname()
            followers=followersname()
            huxiangguanzhu=huxiangguanzhuname(followers,friends)
            n=0
            for follower in followers:
                if follower not in huxiangguanzhu:
                    n+=1
                    print follower
            print '共有'+str(n)+'人他关注你而你没关注他'
            q=raw_input('按"q"键返回上级菜单:')
            if q=='q':
                continue
        elif number=='4':
            print '正在分析，请稍后……'
            friendsgender=get('http://api.t.sina.com.cn/statuses/friends.json',name,password,'gender')         #获取性别
            followersgender=get('http://api.t.sina.com.cn/statuses/followers.json',name,password,'gender')
            friendsmale=friendsgender.count('m')
            friendsfemale=friendsgender.count('f')
            friendsunknown=friendsgender.count('n')
            followersmale=followersgender.count('m')
            followersfemale=followersgender.count('f')
            followersunknown=followersgender.count('n')
            print '**********************'
            print '你关注的人中男女比例为：'
            print '男的占'+str(round(float(friendsmale)/len(friendsgender),4)*100)+'%'
            print '女的占'+str(round(float(friendsfemale)/len(friendsgender),4)*100)+'%'
            print '未知性别占'+str(round(float(friendsunknown)/len(friendsgender),4)*100)+'%'
            print '**********************'
            print '你的粉丝中男女比例为：'
            print '男的占'+str(round(float(followersmale)/len(followersgender),4)*100)+'%'
            print '女的占'+str(round(float(followersfemale)/len(followersgender),4)*100)+'%'
            print '未知性别占'+str(round(float(followersunknown)/len(followersgender),4)*100)+'%'
            print '**********************'
            q=raw_input('按"q"键返回上级菜单:')
            if q=='q':
                continue
    
            
                
            
        



        
        
